package com.jinken.service;

import com.jinken.common.result.BaseResult;
import com.jinken.common.service.IAccountService;
import com.jinken.common.service.IOrderService;
import com.jinken.entity.TOrder;
import com.jinken.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@DubboService
public class OrderServiceImpl implements IOrderService {

    /**
     * 引入账户微服务
     */
    @DubboReference
    private IAccountService accountService;

    @Autowired
    private OrderMapper orderMapper;

    @Transactional
    public BaseResult createOrder(String userId, String commodityCode, int orderCount, int money) {
        //扣减余额
        accountService.debit(userId, money);
        //模拟程序异常
//        System.out.println(1/0);
        //下单
        TOrder order = new TOrder(null, userId, commodityCode, orderCount, money);

        int row = orderMapper.insert(order);
        if (row == 1) {
            return BaseResult.success();
        }
        return BaseResult.error("下单失败，服务异常");
    }
}
